package com.warehouse.controller;

import com.github.pagehelper.PageHelper;
import com.warehouse.dto.Result;
import com.warehouse.dto.ResultCode;
import com.warehouse.entity.PayType;
import com.warehouse.service.PayTypeService;
import com.warehouse.utils.IdWorker;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: warehouse
 * @author: SuRui
 * @create: 2021-01-13 10:16
 * @Version: 1.0
 */
@Slf4j
@Api(tags = "财务管理")
@RestController
@RequestMapping("/api/pay")
public class PayTypeController {
    //注入
    @Autowired
    private PayTypeService payTypeService;

    @ApiOperation(value = "查询全部财务类别")
    @RequestMapping(value = "/types",method = RequestMethod.GET)
    public Result findAll(){
        PageHelper.startPage(1,2);
        List list = payTypeService.findAll();
        log.info("--list"+list);
            return new Result<>(ResultCode.SUCCESS,list);

    }

    @Autowired
    private IdWorker idWorker;
    @ApiOperation(value ="新增" )
    @RequestMapping(value = "/addtype",method = RequestMethod.POST)
    public ResultCode addType(@RequestBody PayType payType){
        //任意类型后加string类型,变为string类型
        String id = idWorker.nextId()+"";
        System.out.println(id);
        payType.setId(id);
        payTypeService.insertPayType(payType);
        return ResultCode.SUCCESS;
    }

    @ApiOperation(value = "删除")
    @RequestMapping(value = "/deltype",method = RequestMethod.DELETE)
    public ResultCode delType(@RequestParam("id") String id){
        log.info("=======================id"+id);
        boolean flag = payTypeService.delPayTypeById(id);
        if (flag){
            return ResultCode.SUCCESS;
        }
        else {
            return ResultCode.FAIL;
        }
    }
    @ApiOperation(value = "修改")
    @RequestMapping(value = "edit",method = RequestMethod.PUT)
    public ResultCode editType(String id,@RequestBody PayType payType){
        payType.setId(id);
        payTypeService.updatePayType(payType);
        return ResultCode.SUCCESS;
    }
    @ApiOperation(value ="查询类别名称" )
    @RequestMapping(value = "/type",method = RequestMethod.GET)
    public Result findType(String name){
        List list =payTypeService.findPayTypeByName(name);
        if (list!=null){
            return new Result(ResultCode.SUCCESS,list);
        }
        return new Result(1000,"w",true);

    }
    @ApiOperation(value = "分页查询全部财务类别")
    @RequestMapping(value = "/pagetypes",method = RequestMethod.GET)
    public Result findPageType(@RequestParam Integer currentPage,Integer pagesize){
        List<PayType>  payTypes = payTypeService.findPageType(currentPage,pagesize);
        if (payTypes !=null){
            return new Result(ResultCode.SUCCESS,payTypes);
        }
        else
            return new Result(ResultCode.FAIL,null);
    }

}
